﻿#include "MaintainLoging.h"
#include "ui_maintainloging.h"

MaintainLoging::MaintainLoging(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MaintainLoging)
{
    ui->setupUi(this);
    ui->labelErrorHint->hide();
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");

    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

    countdown = new CountDownTime(ui->frameMid);
    countdown->setGeometry(710, 10, 30, 40);
    countdown->mysetText("60");

    connect(this->countdown, SIGNAL(CoundDownTimeisOver()), this, SLOT(countdownOver()));
    connect(stackLayout, SIGNAL(currentChanged(int)), this, SLOT(countStart(int)));

    keyBoardConnect();
}

MaintainLoging::~MaintainLoging()
{
    delete ui;
}

void MaintainLoging::countStart(int current)
{
    if(current == deviceMaintaiceforLogingPage)
    {
        countdown->CountDownStart();
    }
}
void MaintainLoging::countdownOver()
{
    /*待完成*/
    Init_Focus();
}


void MaintainLoging::on_buttonOk_clicked()
{
    /*待完成*/
    if( newPassword.restore() )
    {
        passwrd = newPassword.password();
    }
    else
    {
        passwrd = "123456";
    }

    if( ui->maintaince->text() == passwrd )
    {
        stackLayout->setCurrentIndex(SystemSetting1Page); //如果维护密码正确,则进入到系统设置的第一页;
        ui->maintaince->clear();
        countdown->CountDownStop();
    }
    else
    {
        ui->labelErrorHint->show();
        QTimer::singleShot(3000,ui->labelErrorHint,SLOT(hide()));//显示三秒后隐藏
    }
}

void MaintainLoging::on_buttonBack_clicked()
{
    /*待完成*/
    Init_Focus();
    countdown->CountDownStop();
}

void MaintainLoging::on_Keyboard_clicked()
{
    QPushButton* btn =  static_cast<QPushButton*>(sender());

    QLineEdit* focus = (QLineEdit* )QApplication::focusWidget();

    keyHandle(btn, focus);

}

void MaintainLoging::keyHandle(QPushButton* btn, QLineEdit* lineEdit)
{
    if( btn->text() == tr("空格") )
    {
        lineEdit->insert(" ");
    }
    else if( btn->text() == tr("退格") )
    {
        lineEdit->backspace();
    }
    else if( btn->text() == tr("清除"))
    {
        lineEdit->clear();
    }
    else
    {
        lineEdit->insert(btn->text());
    }
}

void MaintainLoging::keyBoardConnect()
{
    connect(ui->One, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Tow, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Three, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Four, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Five, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Six, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Seven, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Eight, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Nine, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Zero, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Remove, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->RemoveAll, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
//    connect(ui->Char1, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
//    connect(ui->Char2, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
//    connect(ui->Space, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
}

